package com.yugao.fintech.antelope.pay.payment.channel;

import com.alibaba.fastjson.JSONObject;
import com.yugao.fintech.antelope.pay.common.utils.ChannelCertConfigKitBean;
import com.yugao.fintech.antelope.pay.constant.IfCodeEnum;
import com.yugao.fintech.antelope.pay.payment.beans.RequestKitBean;
import com.yugao.fintech.antelope.pay.payment.service.ConfigContextQueryService;
import com.yugao.fintech.framework.assistant.utils.exception.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 实现回调接口抽象类
 *

 */
@Slf4j
public abstract class AbstractChannelNoticeService implements IChannelNoticeService {

    @Autowired
    private RequestKitBean requestKitBean;
    @Autowired
    private ChannelCertConfigKitBean channelCertConfigKitBean;
    @Autowired
    protected ConfigContextQueryService configContextQueryService;

    private static final Map<IfCodeEnum, IChannelNoticeService> channelNoticeServiceMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void init() {
        IfCodeEnum ifCode = this.getIfCode();
        Assert.notNull(ifCode, "ifCode is null, class: " + this.getClass().getName());
        channelNoticeServiceMap.put(ifCode, this);
        log.info("load notice service ==> {}", this.getClass().getSimpleName());
    }

    public static IChannelNoticeService getService(IfCodeEnum ifCodeEnum) {
        return channelNoticeServiceMap.get(ifCodeEnum);
    }

    public static IChannelNoticeService getService(String ifCode) {
        return channelNoticeServiceMap.get(IfCodeEnum.ofByCode(ifCode));
    }

    @Override
    public ResponseEntity<?> doNotifyOrderNotExists(HttpServletRequest request) {
        return textResp("order not exists");
    }

    @Override
    public ResponseEntity<?> doNotifyOrderStateUpdateFail(HttpServletRequest request) {
        return textResp("update status error");
    }

    @Override
    public ResponseEntity<?> doNotifyOrderSuccess(HttpServletRequest request) {
        return textResp("update status success");
    }

    /**
     * 文本类型的响应数据
     */
    protected ResponseEntity<?> textResp(String text) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.TEXT_HTML);
        return new ResponseEntity<>(text, httpHeaders, HttpStatus.OK);
    }

    /**
     * json类型的响应数据
     */
    protected ResponseEntity<?> jsonResp(Object body) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<>(body, httpHeaders, HttpStatus.OK);
    }


    /**
     * request.getParameter 获取参数 并转换为JSON格式
     */
    protected JSONObject getReqParamJSON() {
        return requestKitBean.getReqParamJSON();
    }

    /**
     * request.getParameter 获取参数 并转换为JSON格式
     */
    protected String getReqParamFromBody() {
        return requestKitBean.getReqParamFromBody();
    }

    /**
     * 获取文件路径
     */
    protected String getCertFilePath(String certFilePath) {
        return channelCertConfigKitBean.getCertFilePath(certFilePath);
    }

    /**
     * 获取文件File对象
     */
    protected File getCertFile(String certFilePath) {
        return channelCertConfigKitBean.getCertFile(certFilePath);
    }

}
